package com.bsy.videostation.ui.fragment

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bsy.framework.base.BaseMvvmFragment
import com.bsy.framework.ext.onClick
import com.bsy.framework.helper.AppHelper
import com.bsy.videostation.adapter.VideoListAdapter
import com.bsy.videostation.adapter.lisener.OnItemClickListener
import com.bsy.videostation.manager.UserManager
import com.bsy.videostation.model.VideoEntity
import com.bsy.videostation.ui.activity.WebViewActivity
import com.bsy.videostation.view_type.AllViewType
import com.bsy.videostation.view_type.CategoryViewType
import com.bsy.videostation.view_type.HomeViewType
import com.bsy.videostation.viewmodel.MainViewModel
import com.bsy.videostation.viewmodel.VideoViewModel
import com.dg.videostation.databinding.FragmentVideoListBinding
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.internal.CustomAdapt

/**
 * Created by since on 2024/6/2.
 * Copyright
 * 描述：
 */
class VideoListFragment : BaseMvvmFragment<FragmentVideoListBinding, VideoViewModel>(), CustomAdapt, OnItemClickListener<VideoEntity> {
    private val mAdapter: VideoListAdapter by lazy {
        VideoListAdapter()
    }
    private var mainViewModel: MainViewModel? = null
    override fun initViewModel() {
        super.initViewModel()
        mainViewModel = AppHelper.getApplicationViewModel(MainViewModel::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        AutoSize.autoConvertDensity(activity, 1366F, true)
        return getContentView(inflater, container)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        AutoSize.autoConvertDensity(activity, 1366F, true)
        super.onConfigurationChanged(newConfig)
    }

    override fun onResume() {
        AutoSize.autoConvertDensity(activity, 1366F, true)
        super.onResume()
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        initRv()
    }


    private fun initRv() {
        mBinding?.ivBack?.onClick {
            when (index) {
                0 -> mainViewModel?.historyEvent?.value = HomeViewType.HOME_PAGE.value
                1 -> mainViewModel?.categoryEvent?.value = CategoryViewType.HOME_PAGE.value
                2 -> mainViewModel?.allEvent?.value = AllViewType.HOME_PAGE.value
                else -> mainViewModel?.historyEvent?.value = HomeViewType.HOME_PAGE.value
            }

        }
        mBinding?.rvContent?.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            this.setHasFixedSize(true)
        }
    }

    override fun initData() {
        if (mainViewModel?.categorySelectId.isNullOrEmpty())
            mViewModel.videoList.value = mutableListOf()
        else
            mViewModel.getAppInfoList(mainViewModel?.categorySelectId)
        mainViewModel?.categorySelectId = ""
    }

    override fun initObserver() {
        mViewModel.videoList.observe(viewLifecycleOwner) {
            if (mBinding?.rvContent?.adapter == null)
                mBinding?.rvContent?.adapter = mAdapter
            mAdapter.setList(it)
        }
    }

    override fun initListener() {
        mAdapter.setCustomOnItemClickListener(this)

    }

    companion object {
        var index = 0
        fun newInstance(index: Int): VideoListFragment {
            this.index = index
            return VideoListFragment()
        }
    }

    override fun isBaseOnWidth(): Boolean {
        return true
    }

    override fun getSizeInDp(): Float {
        return 1366F
    }

    override fun onClick(position: Int, item: VideoEntity) {
        if (item.detail.isNullOrEmpty()) {
            showToast("No play address found")
            return
        }
        item.time = System.currentTimeMillis()
        UserManager().addHistory(item)
        val intent = Intent(context ?: requireContext(), WebViewActivity::class.java)
        val bundle = Bundle().apply {
            putString("url", item.detail)
        }
        intent.putExtras(bundle)
        startActivity(intent)
    }
}